// 引入Sequelize模块
var bcrypt = require('bcryptjs');
const Sequelize = require('sequelize');
const artPPT = require('./artPPT');
const user = require('./user');

// 引入数据库实例
const db = require('../db');
// 定义model
const order = db.define('order', {
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    sellerId: {
        type: Sequelize.STRING(20),
        comment: "卖家id",
    },

    buyerId: {
        type: Sequelize.STRING(20),
        comment: "买家id",
    },

    pptId: {
        type: Sequelize.STRING(20),
    },

    out_trade_no: {
        type: Sequelize.STRING(200),
        comment: "微信支付订单号",
        unique: true,
    },

    payMoney: {
        type: Sequelize.FLOAT,
        comment: "支付金额",
        defaultValue: 0,
    },

    storeMoney: {
        type: Sequelize.FLOAT,
        comment: "获取金额",
        defaultValue: 0,
    },

    status: {
        type: Sequelize.INTEGER,
        comment: "支付状态",
        defaultValue: 0,
    },

    type: {
        type: Sequelize.INTEGER,
        comment: "单据类型",
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
    },
}, {
    // 是否支持驼峰
    underscored: false,
    // MySQL数据库表名
    tableName: 'order',
});

order.belongsTo(artPPT, {
    foreignKey: 'pptId',
    as: 'ppt',
    onDelete: 'SET NULL',
});

order.belongsTo(user.user, {
    foreignKey: 'buyerId',
    as: "buyer",
    onDelete: 'SET NULL',
    onUpdate: 'SET NULL',
});

order.belongsTo(user.user, {
    foreignKey: 'sellerId',
    as: "seller",
    onDelete: 'SET NULL',
    onUpdate: 'SET NULL',
});

/*(async () => {
    await order.sync({
        alter: true,
    });
    console.log("订单字段同步成功！");
})();*/


module.exports = {
    order
};
